<template>
  <el-dialog
    :title="!dataForm.groupBuyingId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团购人" prop="groupBuyingPeople">
      <el-input v-model="dataForm.groupBuyingPeople" placeholder="团购人"></el-input>
    </el-form-item>
    <el-form-item label="团购标题" prop="groupBuyingTitle">
      <el-input v-model="dataForm.groupBuyingTitle" placeholder="团购标题"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="groupBuyingAddress">
      <el-input v-model="dataForm.groupBuyingAddress" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="开始日期" prop="groupBuyingStartdate">
      <el-input v-model="dataForm.groupBuyingStartdate" placeholder="开始日期"></el-input>
    </el-form-item>
    <el-form-item label="结束日期" prop="groupBuyingEnddate">
      <el-input v-model="dataForm.groupBuyingEnddate" placeholder="结束日期"></el-input>
    </el-form-item>
    <el-form-item label="团购介绍" prop="groupBuyingIntroduction">
      <el-input v-model="dataForm.groupBuyingIntroduction" placeholder="团购介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          groupBuyingId: 0,
          groupBuyingPeople: '',
          groupBuyingTitle: '',
          groupBuyingAddress: '',
          groupBuyingStartdate: '',
          groupBuyingEnddate: '',
          groupBuyingIntroduction: ''
        },
        dataRule: {
          groupBuyingPeople: [
            { required: true, message: '团购人不能为空', trigger: 'blur' }
          ],
          groupBuyingTitle: [
            { required: true, message: '团购标题不能为空', trigger: 'blur' }
          ],
          groupBuyingAddress: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          groupBuyingStartdate: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          groupBuyingEnddate: [
            { required: true, message: '结束日期不能为空', trigger: 'blur' }
          ],
          groupBuyingIntroduction: [
            { required: true, message: '团购介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.groupBuyingId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.groupBuyingId) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/groupbuying/info/${this.dataForm.groupBuyingId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.groupBuyingPeople = data.groupBuying.groupBuyingPeople
                this.dataForm.groupBuyingTitle = data.groupBuying.groupBuyingTitle
                this.dataForm.groupBuyingAddress = data.groupBuying.groupBuyingAddress
                this.dataForm.groupBuyingStartdate = data.groupBuying.groupBuyingStartdate
                this.dataForm.groupBuyingEnddate = data.groupBuying.groupBuyingEnddate
                this.dataForm.groupBuyingIntroduction = data.groupBuying.groupBuyingIntroduction
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/groupbuying/${!this.dataForm.groupBuyingId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'groupBuyingId': this.dataForm.groupBuyingId || undefined,
                'groupBuyingPeople': this.dataForm.groupBuyingPeople,
                'groupBuyingTitle': this.dataForm.groupBuyingTitle,
                'groupBuyingAddress': this.dataForm.groupBuyingAddress,
                'groupBuyingStartdate': this.dataForm.groupBuyingStartdate,
                'groupBuyingEnddate': this.dataForm.groupBuyingEnddate,
                'groupBuyingIntroduction': this.dataForm.groupBuyingIntroduction
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
